package hos.table.simple;

import android.graphics.Color;
import android.text.Spanned;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.text.HtmlCompat;

import com.evrencoskun.tableview.ITableView;

import hos.table.BaseTableAdapter;
import hos.table.R;
import hos.table.holder.BaseTableHolder;
import hos.table.model.RowHeader;


public class SimpleRowHeaderViewHolder extends BaseTableHolder<RowHeader> {

    private final boolean isRanking;

    public SimpleRowHeaderViewHolder(@NonNull View itemView, boolean isRanking) {
        super(itemView);
        this.isRanking = isRanking;

    }

    @Override
    public void onConvert(@NonNull BaseTableAdapter adapter, @NonNull View itemView, @Nullable RowHeader model, int columnPosition, int rowPosition) {
        if (model == null) {
            return;
        }
        AppCompatTextView value = itemView.findViewById(R.id.value);
        View dividerRight = itemView.findViewById(R.id.dividerRight);
        View dividerBottom = itemView.findViewById(R.id.dividerBottom);
        if (isRanking) {
            value.setText(String.valueOf(rowPosition + 1));
        } else {
            Object content = model.getContent();
            if (content instanceof Spanned) {
                value.setText((Spanned) model.getContent());
            } else if (content instanceof CharSequence) {
                value.setText((CharSequence) model.getContent());
            } else {
                String contains = String.valueOf(content);
                if (contains.contains(">") || contains.contains("<")) {
                    value.setText(HtmlCompat.fromHtml(contains, HtmlCompat.FROM_HTML_MODE_LEGACY));
                } else {
                    value.setText(String.valueOf(model.getContent()));
                }
            }
        }
        ITableView tableView = adapter.getTableView();
        int separatorColor = tableView.getSeparatorColor();
        dividerRight.setBackgroundColor(separatorColor);
        dividerBottom.setBackgroundColor(separatorColor);
        dividerBottom.setVisibility(View.GONE);
        if (tableView.isShowHorizontalSeparators()) {
            dividerRight.setVisibility(View.VISIBLE);
        } else {
            dividerRight.setVisibility(View.GONE);
        }
        if (rowPosition % 2 == 0) {
            // 偶数 白色
            itemView.setBackgroundColor(Color.WHITE);
        } else {
            itemView.setBackgroundResource(R.color.table_single_bg);
        }
    }

    @Override
    public void setBackgroundColor(int p_nColor) {
//        if (getBindingAdapterPosition() % 2 == 0) {
//            // 偶数 白色
//            root.setBackgroundColor(Color.WHITE);
//        } else {
//            root.setBackgroundResource(R.color.table_single_bg);
//        }
    }
}
